package com.platform.cdcs.fragment.detail;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.TextView;

import com.platform.cdcs.R;
import com.platform.cdcs.model.InvoiceList;
import com.platform.cdcs.model.ProductList;
import com.platform.cdcs.tool.Constant;
import com.sherchen.slidetoggleheader.views.ObservableXListView;
import com.sherchen.slidetoggleheader.views.XListView;
import com.trueway.app.uilib.adapter.EnhancedAdapter;
import com.trueway.app.uilib.fragment.BaseFragment;
import com.trueway.app.uilib.tool.Utils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

/**
 * Created by holytang on 2017/11/3.
 */
public class ProductBelongFragment extends BaseFragment {

    private ObservableXListView listView;
    private ItemAdapter adapter;
    private int pageIndex = 1;
    private ProductList.ProductItem item;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        item = (ProductList.ProductItem) getArguments().getSerializable("model");
        adapter = new ItemAdapter(getContext());
        adapter.addItem(item);
    }

    @Override
    public void initView(View view) {
        initLoadImg(view.findViewById(R.id.load));
        hideThisToolBar(view);
        getToolBar().setNavigationIcon(R.mipmap.icon_back);
        getToolBar().setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getActivity().finish();
            }
        });
        setTitle("产品归属");
        listView = (ObservableXListView) view.findViewById(android.R.id.list);
//        listView.setXListViewListener(new XListView.IXListViewListener() {
//            @Override
//            public void onRefresh() {
//                pageIndex = 1;
//                adapter.clear();
//                adapter.notifyDataSetChanged();
//                requestList(false);
//            }
//
//            @Override
//            public void onLoadMore() {
//                requestList(false);
//            }
//        });
        listView.setPullRefreshEnable(false);
        listView.setAdapter(adapter);
        listView.setPullRefreshEnable(false);

//        requestList(true);
    }

//    private void requestList(boolean flag) {
//        showLoadImg();
//        Map<String, String> param = new HashMap<>();
//        param.put("productId", item.getProductId());
//        param.put("cusName", item.getCusName());
//        param.put("distName", item.getDistName());
//        param.put("serialNumber", item.getSerialNumber());
//        param.put("inNO", "");
//        param.put("pageIndex", String.valueOf(pageIndex));
//        param.put("pageSize", String.valueOf(Constant.PAGE_SIZE));
//
//        getHttpClient().post().url(Constant.OUT_HOUSELIST).params(Constant.makeParam(param)).build().execute(new StringCallback() {
//            @Override
//            public void onError(Call call, Exception e, int i) {
//                dismissLoadImg();
//                Utils.showToast(getActivity(), R.string.server_error);
//
//            }
//
//            @Override
//            public void onResponse(String s, int i) {
//                dismissLoadImg();
//            }
//        });
//    }

    @Override
    public int layoutId() {
        return R.layout.listview;
    }

    private class ItemAdapter extends EnhancedAdapter<ProductList.ProductItem> {

        public ItemAdapter(Context context) {
            super(context);
        }

        @Override
        protected void bindView(View paramView, Context paramContext, int position) {
            ProductList.ProductItem item = getItem(position);
            ViewHolder holder = (ViewHolder) paramView.getTag();
            holder.title1.setText(item.getCusName());
            holder.text1.setText(item.getStatus());
            holder.text2.setText(item.getWhName());
            holder.text3.setText(item.getQty()+" "+item.getUom());
            holder.title3.setText(item.getDistName());
        }

        @Override
        protected View newView(Context context, int position, ViewGroup viewgroup) {
            View rootView = inflater.inflate(R.layout.belong_product_item, viewgroup, false);
            ViewHolder holder = new ViewHolder();
            holder.title1 = (TextView) rootView.findViewById(R.id.title);
            holder.title2 = (TextView) rootView.findViewById(R.id.title1);
            holder.title3 = (TextView) rootView.findViewById(R.id.title2);
            holder.text1 = (TextView) rootView.findViewById(R.id.text);
            holder.text2 = (TextView) rootView.findViewById(R.id.text1);
            holder.text3 = (TextView) rootView.findViewById(R.id.text2);
            holder.text4 = (TextView) rootView.findViewById(R.id.text3);
            rootView.setTag(holder);
            return rootView;
        }
    }

    private class ViewHolder {
        TextView title1, title2, title3, text1, text2, text3, text4;
    }

}
